/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
public class Keben {
    public List<TreeNode> generateTrees(int n) {
        if (n == 0){
            return new ArrayList<>();
        }
        return generateTrees(1, n);
    }

    public List<TreeNode> generateTrees(int s, int e) {
        List<TreeNode> trees = new ArrayList<>();
        if(s > e){
            trees.add(null);
            return trees;
        }
        for (int i = s; i <= e; i++) {
            List<TreeNode> ls = generateTrees(s, i - 1);
            List<TreeNode> rs = generateTrees(i + 1, e);
            for (int m = 0; m < ls.size(); m++) {
                for (int n = 0; n < rs.size(); n++) {
                    trees.add(new TreeNode(i, ls.get(m), rs.get(n)));
                }
            }
        }
        return trees;
    }
}